//
//  UIViewBase.m
//  HelloWorld
//
//  Created by Mac Book-1 on 10-7-2.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UIViewBase.h"

@implementation UIViewBase

@synthesize interval;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
		interval = 0;
    }
    return self;
}

- (void)addSubview:(UIView *)view{
	[super addSubview:view];
	[self adjustFrame:view];
}

- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview{
	[super insertSubview:view belowSubview:siblingSubview];
	[self adjustFrame:view];
}

- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview{
	[super insertSubview:view aboveSubview:siblingSubview];
	[self adjustFrame:view];
}

- (void)layoutSubviews{
	int count = [self.subviews count];
	UIView* subView = nil;
	CGRect rect;
	CGPoint pt;
	int x = 0;
	int y = 0;
	for (int i = 0; i < count; i++) {
		subView = [self.subviews objectAtIndex:i];
		rect = [subView bounds];
		subView.bounds = rect;
		if ([subView isKindOfClass:[UIViewBase class]]) {
			((UIViewBase*)subView).iAbsoluteY = self.iAbsoluteY + y;
		}
		pt = CGPointMake(x + rect.size.width / 2, y + rect.size.height / 2);
		subView.center = pt;
		y += rect.size.height;
		y += interval;
	}
}

- (void)adjustFrame:(UIView *)view{
	int count = [self.subviews count];
	CGRect rect;
	
	if (count == 1) {
		rect = self.bounds;
		rect.size.height = view.bounds.size.height;
	}
	else {
		rect = self.bounds;
		rect.size.height += interval;
		rect.size.height += view.bounds.size.height;
	}
	self.bounds = rect;
}

@end
